package com.tender.integration.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableConfigurationProperties({RedisShiroProperties.class})
public class RedisConfig {

    @Autowired
    private RedisShiroProperties redisShiroProperties;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws Exception {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        //JSON序列化配置
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        //使用 Jackson2JsonRedisSerializer 来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //String序列化配置
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //hash的key采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        //value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        //开启事务
        template.setEnableTransactionSupport(true);

        return template;
    }

//
//    @Bean
//    public RedissonClient redissonClient() {
//        // 创建配置对象
//        Config config = new Config();
//        config.useSingleServer().setAddress("redis://" + redisShiroProperties.getHost() + ":" + redisShiroProperties.getPort())
//                .setPassword(redisShiroProperties.getPassword());
//
//        // 创建 Redisson 客户端实例
//        return Redisson.create(config);

        // 1、单节点模式
//        Config config = new Config();
//        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
//        RedissonClient redisson = Redisson.create(config);

        // 2、主从模式：可以配置一个主节点和多个从节点
//        Config config = new Config();
//        config.useMasterSlaveServers()
//                .setMasterAddress("redis://master_addr:6379")
//                .addSlaveAddress("redis://slave1_addr:6379", "redis://slave2_addr:6379");
//        RedissonClient redisson = Redisson.create(config);

        // 3、哨兵模式：在这种模式下，你可以配置一组哨兵节点来监视Redis主节点。
//        Config config = new Config();
//        config.useSentinelServers()
//                .addSentinelAddress("redis://sentinel1_addr:26379", "redis://sentinel2_addr:26379")
//                .setMasterName("mymaster");
//        RedissonClient redisson = Redisson.create(config);

        // 4、Config config = new Config();
        //config.useClusterServers()
        //    .addNodeAddress("redis://node1_addr:7001", "redis://node2_addr:7001", "redis://node3_addr:7001");
        //RedissonClient redisson = Redisson.create(config);
//    }


}